﻿
CREATE FUNCTION [dbo].[Distance](
	@Lat1 float,@Lon1 float,
	@Lat2 float,@Lon2 float,
	@EarthRadius numeric(18,9) = 3963.19
)RETURNS numeric(18,9) AS BEGIN
IF @EarthRadius IS NULL SET @EarthRadius = 3963.19
SELECT @Lat1 = RADIANS(@Lat1),@Lon1 = RADIANS(@Lon1)
SELECT @Lat2 = RADIANS(@Lat2),@Lon2 = RADIANS(@Lon2)
RETURN @EarthRadius*ACOS(SIN(@Lat1)*SIN(@Lat2)+COS(@lat1)*COS(@lat2)*COS(@lon2-@lon1))
END

